package com.kaiupup.study.servicePassengerUser.service;

import com.kaiupup.study.dto.PassengerUser;
import com.kaiupup.study.dto.ResponseResult;
import com.kaiupup.study.servicePassengerUser.mapper.PassengerUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: kaiupup-online-taxi
 * @package: com.kaiupup.study.servicepassengeruser.service
 * @className: UserService
 * @author: kaiupup
 * @description: TODO
 * @date: 2024/3/4 19:07
 * @version: 1.0
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private PassengerUserMapper passengerUserMapper;

    public ResponseResult loginOrRegisterUser(String passengerPhone) {

        log.info("UserService-loginOrRegisterUser-手机号:{}", passengerPhone);

        //根据手机号查询用户信息
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone", passengerPhone);
        List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);

        //判断用户信息是否存在
        if (passengerUsers.size() == 0) {
            //如果不存在,插入用户信息
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerName("张三");
            passengerUser.setPassengerGender((byte) 0);
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setState((byte) 0);

            LocalDateTime now = LocalDateTime.now();
            passengerUser.setGmtCreate(now);
            passengerUser.setGmtModified(now);

            passengerUserMapper.insert(passengerUser);

        }

        return ResponseResult.success();

    }

}
